/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.IOException;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.midlet.*;

/**
 * @author Thuan
 */
public class MainMidlet extends MIDlet implements CommandListener {

    //Bien khoi tao
    LoadScreen Load;
    Menu menu;
    Game GAMELV1;
    Game GAMELV2;
    int CurrentLV;
    Command cmdExit = new Command("Exit", Command.EXIT, 0);
    Command nextState = new Command("Qua màn", Command.OK, 0);
    Command startGame = new Command("Bắt đầu", Command.OK, 0);
    Command startAgain = new Command("Chơi lại", Command.OK, 0);

    public void startApp() {

        Load = new LoadScreen();
        Display.getDisplay(this).setCurrent(Load);
        Load.start();


        /*menu = new Menu(this);
        menu.Draw();
        switchDisplay(null, menu);
	Display.getDisplay(this).setCurrent(menu);
        menu.addCommand(cmdExit);
        menu.setCommandListener(this);*/
        try {
            GAMELV1 = new Game(3);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        CurrentLV = 1;
        switchDisplay(null, GAMELV1);
        Display.getDisplay(this).setCurrent(GAMELV1);
        GAMELV1.addCommand(cmdExit);
        GAMELV1.addCommand(startGame);
        GAMELV1.setCommandListener(this);
    }

    public void switchDisplay(Alert alert, Displayable newDisplayable) {

        Display display = getDisplay();

        if (alert == null) {

            display.setCurrent(newDisplayable);

        } else {
            display.setCurrent(alert, newDisplayable);
        }
    }

    public Display getDisplay() {
        return Display.getDisplay(this);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void commandAction(Command c, Displayable d) {
        if (c == cmdExit) {
            //Thoat chuong trinh
            destroyApp(true);
            notifyDestroyed();
        } else if (c == startGame && GAMELV1.started == false && CurrentLV == 1) {
            GAMELV1.started = true;
            try {
                GAMELV1.Start(1);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            GAMELV1.removeCommand(startGame);
            GAMELV1.addCommand(cmdExit);
            GAMELV1.addCommand(nextState);
            GAMELV1.addCommand(startAgain);
            GAMELV1.setCommandListener(this);
        } else if (c == nextState && GAMELV1.isWinner == true) {
            try {
                GAMELV2 = new Game(5);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            CurrentLV = 2;
            switchDisplay(null, GAMELV2);
            GAMELV2.addCommand(startGame);
            GAMELV2.addCommand(cmdExit);
            GAMELV2.setCommandListener(this);
        } else if (c == startGame && CurrentLV == 2 && GAMELV2.started == false) {
            try {
                GAMELV2.Start(2);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            GAMELV2.addCommand(startAgain);
            GAMELV2.removeCommand(startGame);
        } else if (c == startAgain) {
            CurrentLV = 1;
            try {
                GAMELV1 = new Game(3);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            switchDisplay(null, GAMELV1);
            GAMELV1.addCommand(cmdExit);
            GAMELV1.addCommand(startGame);
            GAMELV1.setCommandListener(this);
            Display.getDisplay(this).setCurrent(GAMELV1);
        }
    }
}
